package com.hxzy.servlet.UmsMemberLoginLog;

import com.hxzy.entity.UmsMemberLoginLog;
import com.hxzy.service.UmsMemberLoginLogService;
import com.hxzy.service.impl.UmsMemberLoginLogServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/UmsMemberLoginLog/insert")
public class insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/UmsMemberLoginLog/insert.jsp").forward(req,res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        UmsMemberLoginLog loginLog=new UmsMemberLoginLog();
        Long memberId=Long.parseLong(req.getParameter("memberId"));
        loginLog.setMemberId(memberId);

        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD");
        try {
            Date createTime=sdf.parse(req.getParameter("createTime"));
            loginLog.setCreateTime(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String ip=req.getParameter("ip");
        loginLog.setIp(ip);

        String city=req.getParameter("city");

        loginLog.setCity(city);
        /**
         * 登录类型：0->PC；1->android;2->ios;3->小程序
         */
        Integer loginType=Integer.parseInt(req.getParameter("loginType"));
        loginLog.setLoginType(loginType);

        String province=req.getParameter("province");
        loginLog.setProvince(province);

        UmsMemberLoginLogService service=new UmsMemberLoginLogServiceImpl();
        boolean insert = service.insert(loginLog);

        PrintWriter out=res.getWriter();
        out.println( insert? "新增成功": "新增失败");
        //输出刷新
        out.flush();
        out.close();
    }
}
